package com.tedu.m5;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/11/30
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.126.129");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");
        Channel channel = c.newConnection().createChannel();
        String queue = channel.queueDeclare().getQueue();
        channel.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);
        System.out.println("输入绑定键，用空格隔开");
        String[] a = new Scanner(System.in).nextLine().split("\\s");

        //把该队列,绑定到 direct_logs 交换机
        //允许使用多个 bindingKey
        for (String level : a) {
            channel.queueBind(queue, "topic_logs", level);
        }

        channel.basicConsume(queue, true, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody());
                System.out.println(String.format("%s:%s",message.getEnvelope().getRoutingKey(),msg));
            }
        }, new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                
            }
        });


    }
}
